package cn.iocoder.yudao.module.vos.controller.admin.datamatchtask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.datamatchtask.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.datamatchtask.DataMatchTaskDO;
import cn.iocoder.yudao.module.vos.service.datamatchtask.DataMatchTaskService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 数据匹配下载")
@RestController
@RequestMapping("/vos/data-match-task")
@Validated
public class DataMatchTaskController {

    @Resource
    private DataMatchTaskService dataMatchTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建数据匹配下载")
    @PreAuthorize("@ss.hasPermission('vos:data-match-task:create')")
    public CommonResult<Long> createDataMatchTask(@RequestParam("file") MultipartFile file, @Valid @ModelAttribute DataMatchTaskSaveReqVO createReqVO) throws IOException {
        return success(dataMatchTaskService.createDataMatchTask(file,createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据匹配下载")
    @PreAuthorize("@ss.hasPermission('vos:data-match-task:update')")
    public CommonResult<Boolean> updateDataMatchTask(@Valid @RequestBody DataMatchTaskSaveReqVO updateReqVO) {
        dataMatchTaskService.updateDataMatchTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据匹配下载")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:data-match-task:delete')")
    public CommonResult<Boolean> deleteDataMatchTask(@RequestParam("id") Long id) {
        dataMatchTaskService.deleteDataMatchTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据匹配下载")
                @PreAuthorize("@ss.hasPermission('vos:data-match-task:delete')")
    public CommonResult<Boolean> deleteDataMatchTaskList(@RequestParam("ids") List<Long> ids) {
        dataMatchTaskService.deleteDataMatchTaskListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据匹配下载")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:data-match-task:query')")
    public CommonResult<DataMatchTaskRespVO> getDataMatchTask(@RequestParam("id") Long id) {
        DataMatchTaskDO dataMatchTask = dataMatchTaskService.getDataMatchTask(id);
        return success(BeanUtils.toBean(dataMatchTask, DataMatchTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据匹配下载分页")
    @PreAuthorize("@ss.hasPermission('vos:data-match-task:query')")
    public CommonResult<PageResult<DataMatchTaskRespVO>> getDataMatchTaskPage(@Valid DataMatchTaskPageReqVO pageReqVO) {
        PageResult<DataMatchTaskDO> pageResult = dataMatchTaskService.getDataMatchTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataMatchTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据匹配下载 Excel")
    @PreAuthorize("@ss.hasPermission('vos:data-match-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataMatchTaskExcel(@Valid DataMatchTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataMatchTaskDO> list = dataMatchTaskService.getDataMatchTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据匹配下载.xls", "数据", DataMatchTaskRespVO.class,
                        BeanUtils.toBean(list, DataMatchTaskRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<BatchDownloadImportExcelVO> list = Arrays.asList(BatchDownloadImportExcelVO.builder().phone("552318797682855").build());
        // 输出
        ExcelUtils.write(response, "批量下载导入模板.xls", "用户列表", BatchDownloadImportExcelVO.class, list);
    }

}